package com.minisoft.utils;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.IdUtil;
import com.obs.services.IObsClient;
import com.obs.services.ObsClient;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.io.*;
import java.net.URLEncoder;
import java.time.LocalDateTime;
@Slf4j
public class HuaweicloudClient {

    @Setter
    private String endpoint = "obs.cn-south-1.myhuaweicloud.com";
    @Setter
    private String accessKeyId = "SASXE9KPIICBAGNPXIYW";
    @Setter
    private String accessKeySecret = "qU3jCCsvMm24zBjfyFsS94IhOoEyHACU8GAio63q";
    @Setter
    private String bucket = "crmfile";
    /**
     * 文件路径前缀
     */
    @Setter
    private String pathRoot;

    private IObsClient client;
    /**
     * 访问域名
     */
    @Getter
    private String accessDomain;

    @PostConstruct
    public void init() {
        client = new ObsClient(accessKeyId, accessKeySecret, endpoint);
        String str = "https://" + bucket + "." + endpoint;
        accessDomain = str;
    }

    @PreDestroy
    public void destroy() throws IOException {
        if (client != null) {
            client.close();
        }
    }

    public String uploadInputStream(InputStream inputStream, String fileName, String customPathRoot) {
        String key = generateKey(customPathRoot, fileName);
        client.putObject(bucket, key, inputStream);

        StringBuilder str = new StringBuilder(128);
        str.append(this.accessDomain).append("/").append(key);
        return str.toString();
    }

    public String uploadBase64(String base64Str, String fileName) {
        String key = generateKey(null, fileName);
        try {
            // 去掉 base64头信息
            if (base64Str.contains(",")) {
                base64Str = base64Str.substring(base64Str.indexOf(","));
            }
            byte[] b = Base64.decode(base64Str);
            client.putObject(bucket, key, new ByteArrayInputStream(b));
        } catch (Exception e) {
            log.error("上传oss失败", e);
            throw new IllegalArgumentException("图片上传OSS失败");
        }
        StringBuilder str = new StringBuilder(128);
        str.append(this.accessDomain).append("/").append(key);
        return str.toString();
    }

    private String generateKey(String customPathRoot, String fileExtension) {
        String dateStr = LocalDateTime.now().toString();
        String date = dateStr.substring(0, 10);
        StringBuilder str = new StringBuilder(100);
        str.append(StringUtils.defaultIfBlank(customPathRoot, pathRoot))
                .append("/").append(date)
                .append("/").append(IdUtil.fastSimpleUUID())
                .append(".").append(fileExtension);
        return str.toString();
    }

    public static void main(String[] args) throws IOException {
        HuaweicloudClient client = new HuaweicloudClient();
        client.init();
        InputStream input = new FileInputStream("D:\\IP.jpg");
        String url = client.uploadInputStream(input,"jpg","test");
        System.out.println(url);//https://crmfile.obs.cn-south-1.myhuaweicloud.com/test/2024-02-27/bc8c5fb291d74fa395a17016761e10a0.jpg
        client.destroy();
    }

}
